<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="账户号" prop="accountId">
        <el-input
          v-model="queryParams.accountId"
          placeholder="请输入账户号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="realname">
        <el-input
          v-model="queryParams.realname"
          placeholder="请输入姓名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-select v-model="queryParams.sex" placeholder="请选择性别" clearable>
          <el-option
            v-for="dict in user_gender"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['info:info:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['info:info:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['info:info:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['info:info:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="infoList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        label="序号"
        width="70"
        align="center"
        prop="id"
        type="index"
      />
      <el-table-column
        label="账户号"
        width="70"
        align="center"
        prop="accountId"
      />
      <el-table-column label="姓名" width="70" align="center" prop="realname" />
      <el-table-column label="年龄" width="70" align="center" prop="age" />
      <el-table-column label="性别" width="70" align="center" prop="sex" />

      <el-table-column label="身份证号" align="center" prop="cardid" />
      <el-table-column label="地址" align="center" prop="address" />
      <el-table-column label="电话号码" align="center" prop="telephone" />
      <el-table-column
        label="更新时间"
        align="center"
        prop="updateTime"
        width="180"
      >
        <template #default="scope">
          <span>{{
            parseTime(scope.row.updateTime, "{y}-{m}-{d} {h}-{i}-{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="180px"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="getUserAccount(scope.row)"
            v-hasPermi="['system:account:query']"
            >查看详情</el-button
          >

          <el-button
            link
            type="primary"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['info:info:edit']"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            @click="handleDelete(scope.row)"
            v-hasPermi="['info:info:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改信息管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="infoRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="realname">
          <el-input v-model="form.realname" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="form.age" placeholder="请输入年龄" />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择性别">
            <el-option
              v-for="dict in user_gender"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="身份证号" prop="cardid">
          <el-input v-model="form.cardid" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="用户地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入用户地址" />
        </el-form-item>
        <el-form-item label="电话号码" prop="telephone">
          <el-input v-model="form.telephone" placeholder="请输入电话号码" />
        </el-form-item>
        <el-divider content-position="center">用户账户信息信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="Plus" @click="handleAddAccount"
              >添加</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="Delete" @click="handleDeleteAccount"
              >删除</el-button
            >
          </el-col>
        </el-row>
        <el-table
          :data="accountList"
          :row-class-name="rowAccountIndex"
          @selection-change="handleAccountSelectionChange"
          ref="account"
        >
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column
            label="序号"
            align="center"
            prop="index"
            width="50"
          />
          <el-table-column label="用户的登录名" prop="username" width="150">
            <template #default="scope">
              <el-input
                v-model="scope.row.username"
                placeholder="请输入用户的登录名"
              />
            </template>
          </el-table-column>
          <el-table-column label="用户密码" prop="password" width="150">
            <template #default="scope">
              <el-input
                v-model="scope.row.password"
                placeholder="请输入用户密码"
              />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 用户账户详情 -->
    <el-dialog title="用户账户详情" v-model="openAc" width="600px">
      <el-descriptions :column="2" border>
        <el-descriptions-item label="登录名">{{
          acList.username
        }}</el-descriptions-item>
        <el-descriptions-item label="用户状态">
          <span v-if="acList.status === 0">未激活</span>
          <span v-else-if="acList.status === 1">正常</span>
          <span v-else>冻结</span>
        </el-descriptions-item>
        <el-descriptions-item label="银行卡号">{{
          acList.cardNumber
        }}</el-descriptions-item>
        <el-descriptions-item label="账户金额">{{
          acList.balance
        }}</el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script setup name="Info">
import {
  listInfo,
  getInfo,
  delInfo,
  addInfo,
  updateInfo,
} from "@/api/info/info";
import { getAccount } from "@/api/account/account.js";

const { proxy } = getCurrentInstance();
const { user_gender } = proxy.useDict("user_gender");

const infoList = ref([]);
const accountList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const checkedAccount = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    accountId: null,
    realname: null,
    sex: null,
  },
  rules: {
    realname: [{ required: true, message: "姓名不能为空", trigger: "blur" }],
    age: [{ required: true, message: "年龄不能为空", trigger: "blur" }],
    sex: [{ required: true, message: "性别不能为空", trigger: "change" }],
    cardid: [{ required: true, message: "身份证号不能为空", trigger: "blur" }],
    address: [{ required: true, message: "用户地址不能为空", trigger: "blur" }],
    telephone: [
      { required: true, message: "电话号码不能为空", trigger: "blur" },
    ],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询信息管理列表 */
function getList() {
  loading.value = true;
  listInfo(queryParams.value).then((response) => {
    infoList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    accountId: null,
    realname: null,
    age: null,
    sex: null,
    cardid: null,
    address: null,
    telephone: null,
    createTime: null,
    updateTime: null,
  };
  accountList.value = [];
  proxy.resetForm("infoRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加信息管理";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value;
  getInfo(_id).then((response) => {
    form.value = response.data;
    accountList.value = response.data.accountList;
    open.value = true;
    title.value = "修改信息管理";
  });
}

/** 查询用户账户下详情信息 */
const acList = ref([]);
const openAc = ref(false);
function getUserAccount(row) {
  getAccount(row.accountId).then((response) => {
    acList.value = response.data;
    console.log(acList.value);
  });
  openAc.value = true;
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["infoRef"].validate((valid) => {
    if (valid) {
      form.value.accountList = accountList.value;
      if (form.value.id != null) {
        updateInfo(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addInfo(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除信息管理编号为"' + _ids + '"的数据项？')
    .then(function () {
      return delInfo(_ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 用户账户信息序号 */
function rowAccountIndex({ row, rowIndex }) {
  row.index = rowIndex + 1;
}

/** 用户账户信息添加按钮操作 */
function handleAddAccount() {
  let obj = {};
  obj.username = "";
  obj.password = "";
  obj.balance = "";
  obj.status = "";
  obj.cardNumber = "";
  accountList.value.push(obj);
}

/** 用户账户信息删除按钮操作 */
function handleDeleteAccount() {
  if (checkedAccount.value.length == 0) {
    proxy.$modal.msgError("请先选择要删除的用户账户信息数据");
  } else {
    const accounts = accountList.value;
    const checkedAccounts = checkedAccount.value;
    accountList.value = accounts.filter(function (item) {
      return checkedAccounts.indexOf(item.index) == -1;
    });
  }
}

/** 复选框选中数据 */
function handleAccountSelectionChange(selection) {
  checkedAccount.value = selection.map((item) => item.index);
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "info/info/export",
    {
      ...queryParams.value,
    },
    `info_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>
